import { Component, Vue } from 'vue-property-decorator';
import Guide from '@guide';
import ProjectFilter from './filter/Index.vue';
import { Menu } from '@entity/common';
import { Project, ProjectTag } from '@entity/project';

@Component({
    components: {
        Guide, ProjectFilter
    }
})
export class JoinProject extends Vue {

    /** 最新岗位 */
    private guideList: Array<Menu> = [
        new Menu("最新岗位1", '#'),
    ]

    /** 项目列表 */
    private projectList: Array<Project> = this.$store.getters.projectList();

    /** 展示在页面上的最终项目列表 */
    private finalSelection: Array<Project> = this.projectList;

    /**
     * 判断某个项目是否包含某个关键字
     * @param project 项目
     * @param searchKeywords 搜索关键字
     */
    private isKeywordsOf(project: Project, searchKeywords: string) {
        return project.name.contain(searchKeywords);
    }

    /**
     * 判断某个项目是否包含某个职位类型
     * @param project 项目
     * @param type 职位类型
     */
    private isWorkTypeOf(project: Project, type: string) {
        for (let index: number = 0; index < project.positions.length; index++) {
            let position: ProjectTag = project.positions[index];
            console.log(`${position.tag}, ${type}`)
            if (position.memberId == -1 && position.tag == type) {
                return true;
            }
        }
        return false;
    }

    /**
     * 当项目筛选器选中的条件发生变化时自动调用
     * 更新显示在页面上的项目列表
     * @param data 筛选器传回的数据
     */
    private onSelectionChange(data: any): void {
        let searchKeywords: string = data.searchKeywords;
        let competition: string = data.competition;
        let workType: string = data.workType;
        this.finalSelection = [];
        this.projectList.forEach(item => {
            console.log(`${item.name}:`)
            let flag1: boolean = searchKeywords.isEmpty() || this.isKeywordsOf(item, searchKeywords);
            let flag2: boolean = workType.isEmpty() || this.isWorkTypeOf(item, workType);
            console.log(`flag1 = ${flag1}, flag2 = ${flag2}`)
            if (flag1 && flag2) {
                this.finalSelection.push(item);
            }
        });
    }
}